﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Universe.Computing.UCSharedLib;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

namespace Universe.Computing.UCHostLib
{
    public class UCHostLib
    {
        public void StartHost()
        {
            IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, UCConfig.Instance.ServerPort);
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            sock.Bind(ipEnd);
            sock.Listen(100);
            while (true)
            {
                Socket clientSock = sock.Accept();
                Console.WriteLine("One User submitted file");
                FileReceiver fr = new FileReceiver();
                fr.ClientSocket = clientSock;
                ThreadStart ts = new ThreadStart(fr.ExecuteJob);
                Thread t = new Thread(ts);
                t.Start();
                if (clientSock != null&&clientSock.Connected)
                {
                    clientSock.Close();
                }
            }
        }
        
    }
}
